﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FW;
using dbiparser;
using System.Configuration;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;


namespace Kcadastro.classes
{
    public class cuni
    {
        private consultaBasicaPJ cbPJ;
        public consultaBasicaPJ CONSULTABASICA {
            get { return cbPJ; }
        }


        private fwlog log = (fwlog)ConfigurationManager.GetSection("fwlog");
        private int idemp;
        kcadDataContext db = new kcadDataContext(ConfigurationManager.ConnectionStrings["KcadConnectionString"].ToString());

        private string _msg;
        public string MSG { get { return _msg; } }

        public cuni(int _IDemp)
        {
            idemp = _IDemp;
        }

        public List<consultaDisponivel> ConsultasDisponiveis()
        {
            List<consultaDisponivel> ret = (from tb in db.tb_consultasDisponiveis
                                            where tb.disponivel == true
                                            orderby tb.PK_IDconsulta
                                            select new consultaDisponivel
                                            {
                                                ID = tb.PK_IDconsulta,
                                                NOME = tb.PK_IDconsulta.ToString() + " - " + tb.nome
                                            }).ToList<consultaDisponivel>();

            return ret;

        }

        public tb_consultasDisponivei ParametrosDaConsulta(string _ID)
        {
            tb_consultasDisponivei ret = db.tb_consultasDisponiveis.Single(t => t.PK_IDconsulta == Convert.ToInt32(_ID));
            return ret;
        }



        private void gravarRegistro()
        {
            try
            {
                FW.cIO cio = new cIO();
                string dataMilitar = DateTime.Now.ToString("yyyyMMdd");
                string caminhoSaida = ConfigurationManager.AppSettings["arqPath"].ToString() + idemp.ToString() + "\\consdiaria" + "\\" + dataMilitar;
                //cio.CriaDir(ConfigurationManager.AppSettings["arqPath"].ToString() + idemp.ToString());
                //cio.CriaDir(ConfigurationManager.AppSettings["arqPath"].ToString() + idemp.ToString() + "\\consdiaria");
                cio.CriaDir(caminhoSaida);

                gerarXLS(caminhoSaida);
            }
            catch (Exception ex)
            {
                log.erro("cuni.gravarRegistro", idemp.ToString(), ex.Message);
                _msg = "Erro encontrado. Favor entrar em contato com o administrador do sistema.";
            }
        }


        private void gerarXLS(string _dirSaida)
        {
            try
            {
                string xlsfile = _dirSaida + "\\" + cbPJ.OR_CNPJ + ".xls";
                FileStream fs = new FileStream(ConfigurationManager.AppSettings["arqPath"].ToString() + "/templateUnitaria.xls", FileMode.Open, FileAccess.Read);

                FileStream msResult = new FileStream(xlsfile, FileMode.Create);

                HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);


                //Resumo
                var sheet = templateWorkbook.GetSheet("resumo");

                sheet.GetRow(11).GetCell(2).SetCellValue(cbPJ.OR_CNPJ);
                sheet.GetRow(12).GetCell(2).SetCellValue(DateTime.Now.ToString("dd/MM/yyyy"));


                //VERTICAL

                sheet = templateWorkbook.GetSheet("VERTICAL");

                sheet.GetRow(2).CreateCell(1).SetCellValue(cbPJ.RFCNPJ);
                sheet.GetRow(3).CreateCell(1).SetCellValue(cbPJ.RFDATAABERTURA);// DateTime.Now.ToString("dd/MM/yyyy"));
                sheet.GetRow(4).CreateCell(1).SetCellValue(cbPJ.RFRAZAOSOCIAL);
                sheet.GetRow(5).CreateCell(1).SetCellValue(cbPJ.RFNOMEFANTASIA);
                sheet.GetRow(6).CreateCell(1).SetCellValue(cbPJ.RFCODIGOAE);
                sheet.GetRow(7).CreateCell(1).SetCellValue(cbPJ.RFDESCRICAOAE);
                sheet.GetRow(8).CreateCell(1).SetCellValue(cbPJ.RFCODIGONJ);
                sheet.GetRow(9).CreateCell(1).SetCellValue(cbPJ.RFDESCRICAONJ);
                sheet.GetRow(10).CreateCell(1).SetCellValue(cbPJ.RFLOGRADOURO);
                sheet.GetRow(11).CreateCell(1).SetCellValue(cbPJ.RFNUMERO);
                sheet.GetRow(12).CreateCell(1).SetCellValue(cbPJ.RFCOMPLEMENTO);
                sheet.GetRow(13).CreateCell(1).SetCellValue(cbPJ.RFCEP);
                sheet.GetRow(14).CreateCell(1).SetCellValue(cbPJ.RFBAIRRO);
                sheet.GetRow(15).CreateCell(1).SetCellValue(cbPJ.RFCIDADE);
                sheet.GetRow(16).CreateCell(1).SetCellValue(cbPJ.RFESTADO);
                sheet.GetRow(17).CreateCell(1).SetCellValue(cbPJ.RFSITUACAO);
                sheet.GetRow(18).CreateCell(1).SetCellValue(cbPJ.RFDATASITUACAO);
                sheet.GetRow(19).CreateCell(1).SetCellValue(cbPJ.RFSITUACAOESPECIAL);
                sheet.GetRow(20).CreateCell(1).SetCellValue(cbPJ.RFDATASITUACAOESPECIAL);
                sheet.GetRow(21).CreateCell(1).SetCellValue(cbPJ.RFDATACONSULTA);
                sheet.GetRow(22).CreateCell(1).SetCellValue(cbPJ.RFHORACONSULTA);
                sheet.GetRow(23).CreateCell(1).SetCellValue(cbPJ.RFTIPOEMPRESA);
                sheet.GetRow(24).CreateCell(1).SetCellValue(cbPJ.RFMOTIVOSITUACAO);


                sheet.GetRow(27).CreateCell(1).SetCellValue(cbPJ.SICNPJ);
                sheet.GetRow(28).CreateCell(1).SetCellValue(cbPJ.SIIE);
                sheet.GetRow(29).CreateCell(1).SetCellValue(cbPJ.SIRAZAOSOCIAL);
                sheet.GetRow(30).CreateCell(1).SetCellValue(cbPJ.SINOMEFANTASIA);
                sheet.GetRow(31).CreateCell(1).SetCellValue(cbPJ.SILOGRADOURO);
                sheet.GetRow(32).CreateCell(1).SetCellValue(cbPJ.SINUMERO);
                sheet.GetRow(33).CreateCell(1).SetCellValue(cbPJ.SICOMPLEMENTO);
                sheet.GetRow(34).CreateCell(1).SetCellValue(cbPJ.SIBAIRRO);
                sheet.GetRow(35).CreateCell(1).SetCellValue(cbPJ.SICIDADE);
                sheet.GetRow(36).CreateCell(1).SetCellValue(cbPJ.SIESTADO);
                sheet.GetRow(37).CreateCell(1).SetCellValue(cbPJ.SICEP);
                sheet.GetRow(38).CreateCell(1).SetCellValue(cbPJ.SIEMAIL);
                sheet.GetRow(39).CreateCell(1).SetCellValue(cbPJ.SISITUACAO);
                sheet.GetRow(40).CreateCell(1).SetCellValue(cbPJ.SIDATAINCLUSAO);
                sheet.GetRow(41).CreateCell(1).SetCellValue(cbPJ.SIDATABAIXA);
                sheet.GetRow(42).CreateCell(1).SetCellValue(cbPJ.SIDATACONSULTA);
                sheet.GetRow(43).CreateCell(1).SetCellValue(cbPJ.SINUMEROCONSULTA);
                sheet.GetRow(44).CreateCell(1).SetCellValue(cbPJ.SIREGIMEAPURACAO);
                sheet.GetRow(45).CreateCell(1).SetCellValue(cbPJ.SIMOTIVOBAIXA);
                sheet.GetRow(46).CreateCell(1).SetCellValue(cbPJ.SIREGULARIDADEFISCAL);

                sheet.GetRow(49).CreateCell(1).SetCellValue(cbPJ.SNCNPJ);
                sheet.GetRow(50).CreateCell(1).SetCellValue(cbPJ.SNRAZAOSOCIAL);
                sheet.GetRow(51).CreateCell(1).SetCellValue(cbPJ.SNSITUACAO);
                sheet.GetRow(52).CreateCell(1).SetCellValue(cbPJ.SNDATASITUACAO);
                sheet.GetRow(53).CreateCell(1).SetCellValue(cbPJ.SNSITUACAOSIMEI);
                sheet.GetRow(54).CreateCell(1).SetCellValue(cbPJ.SNSITUACAOANTERIOR);

                sheet.GetRow(57).CreateCell(1).SetCellValue(cbPJ.CORUA);
                sheet.GetRow(58).CreateCell(1).SetCellValue(cbPJ.COCOMPLEMENTO);
                sheet.GetRow(59).CreateCell(1).SetCellValue(cbPJ.COBAIRRO);
                sheet.GetRow(60).CreateCell(1).SetCellValue(cbPJ.COCIDADE);
                sheet.GetRow(61).CreateCell(1).SetCellValue(cbPJ.COESTADO);
                sheet.GetRow(62).CreateCell(1).SetCellValue(cbPJ.COCEPNUM);
                sheet.GetRow(63).CreateCell(1).SetCellValue(cbPJ.COERRO);
                sheet.GetRow(64).CreateCell(1).SetCellValue(cbPJ.COCODLOGRADOURO);
                sheet.GetRow(65).CreateCell(1).SetCellValue(cbPJ.COCODBAIRRO);
                sheet.GetRow(66).CreateCell(1).SetCellValue(cbPJ.COCODLOCALIDADE);
                sheet.GetRow(67).CreateCell(1).SetCellValue(cbPJ.CODIBGE);


                // HORIZONTAL
                sheet = templateWorkbook.GetSheet("HORIZONTAL");

                var row = sheet.CreateRow(1);

                row.CreateCell(0).SetCellValue(cbPJ.RFCNPJ);
                row.CreateCell(1).SetCellValue(cbPJ.RFDATAABERTURA);
                row.CreateCell(2).SetCellValue(cbPJ.RFRAZAOSOCIAL);
                row.CreateCell(3).SetCellValue(cbPJ.RFNOMEFANTASIA);
                row.CreateCell(4).SetCellValue(cbPJ.RFCODIGOAE);
                row.CreateCell(5).SetCellValue(cbPJ.RFDESCRICAOAE);
                row.CreateCell(6).SetCellValue(cbPJ.RFCODIGONJ);
                row.CreateCell(7).SetCellValue(cbPJ.RFDESCRICAONJ);
                row.CreateCell(8).SetCellValue(cbPJ.RFLOGRADOURO);
                row.CreateCell(9).SetCellValue(cbPJ.RFNUMERO);
                row.CreateCell(10).SetCellValue(cbPJ.RFCOMPLEMENTO);
                row.CreateCell(11).SetCellValue(cbPJ.RFCEP);
                row.CreateCell(12).SetCellValue(cbPJ.RFBAIRRO);
                row.CreateCell(13).SetCellValue(cbPJ.RFCIDADE);
                row.CreateCell(14).SetCellValue(cbPJ.RFESTADO);
                row.CreateCell(15).SetCellValue(cbPJ.RFSITUACAO);
                row.CreateCell(16).SetCellValue(cbPJ.RFDATASITUACAO);
                row.CreateCell(17).SetCellValue(cbPJ.RFSITUACAOESPECIAL);
                row.CreateCell(18).SetCellValue(cbPJ.RFDATASITUACAOESPECIAL);
                row.CreateCell(19).SetCellValue(cbPJ.RFDATACONSULTA);
                row.CreateCell(20).SetCellValue(cbPJ.RFHORACONSULTA);
                row.CreateCell(21).SetCellValue(cbPJ.RFTIPOEMPRESA);
                row.CreateCell(22).SetCellValue(cbPJ.RFMOTIVOSITUACAO);
                row.CreateCell(23).SetCellValue(cbPJ.SICNPJ);
                row.CreateCell(24).SetCellValue(cbPJ.SIIE);
                row.CreateCell(25).SetCellValue(cbPJ.SIRAZAOSOCIAL);
                row.CreateCell(26).SetCellValue(cbPJ.SINOMEFANTASIA);
                row.CreateCell(27).SetCellValue(cbPJ.SILOGRADOURO);
                row.CreateCell(28).SetCellValue(cbPJ.SINUMERO);
                row.CreateCell(29).SetCellValue(cbPJ.SICOMPLEMENTO);
                row.CreateCell(30).SetCellValue(cbPJ.SIBAIRRO);
                row.CreateCell(31).SetCellValue(cbPJ.SICIDADE);
                row.CreateCell(32).SetCellValue(cbPJ.SIESTADO);
                row.CreateCell(33).SetCellValue(cbPJ.SICEP);
                row.CreateCell(34).SetCellValue(cbPJ.SIEMAIL);
                row.CreateCell(35).SetCellValue(cbPJ.SISITUACAO);
                row.CreateCell(36).SetCellValue(cbPJ.SIDATAINCLUSAO);
                row.CreateCell(37).SetCellValue(cbPJ.SIDATABAIXA);
                row.CreateCell(38).SetCellValue(cbPJ.SIDATACONSULTA);
                row.CreateCell(39).SetCellValue(cbPJ.SINUMEROCONSULTA);
                row.CreateCell(40).SetCellValue(cbPJ.SIREGIMEAPURACAO);
                row.CreateCell(41).SetCellValue(cbPJ.SIMOTIVOBAIXA);
                row.CreateCell(42).SetCellValue(cbPJ.SIREGULARIDADEFISCAL);
                row.CreateCell(43).SetCellValue(cbPJ.SNCNPJ);
                row.CreateCell(44).SetCellValue(cbPJ.SNRAZAOSOCIAL);
                row.CreateCell(45).SetCellValue(cbPJ.SNSITUACAO);
                row.CreateCell(46).SetCellValue(cbPJ.SNDATASITUACAO);
                row.CreateCell(47).SetCellValue(cbPJ.SNSITUACAOSIMEI);
                row.CreateCell(48).SetCellValue(cbPJ.SNSITUACAOANTERIOR);
                row.CreateCell(49).SetCellValue(cbPJ.CORUA);
                row.CreateCell(50).SetCellValue(cbPJ.COCOMPLEMENTO);
                row.CreateCell(51).SetCellValue(cbPJ.COBAIRRO);
                row.CreateCell(52).SetCellValue(cbPJ.COCIDADE);
                row.CreateCell(53).SetCellValue(cbPJ.COESTADO);
                row.CreateCell(54).SetCellValue(cbPJ.COCEPNUM);
                row.CreateCell(55).SetCellValue(cbPJ.COERRO);
                row.CreateCell(56).SetCellValue(cbPJ.COCODLOGRADOURO);
                row.CreateCell(57).SetCellValue(cbPJ.COCODBAIRRO);
                row.CreateCell(58).SetCellValue(cbPJ.COCODLOCALIDADE);
                row.CreateCell(59).SetCellValue(cbPJ.CODIBGE);

                //RECEITAFEDERAL
                sheet = templateWorkbook.GetSheet("RECEITAFEDERAL");

                sheet.GetRow(1).CreateCell(1).SetCellValue(cbPJ.RFCNPJ);
                sheet.GetRow(2).CreateCell(1).SetCellValue(cbPJ.RFDATAABERTURA);// DateTime.Now.ToString("dd/MM/yyyy"));
                sheet.GetRow(3).CreateCell(1).SetCellValue(cbPJ.RFRAZAOSOCIAL);
                sheet.GetRow(4).CreateCell(1).SetCellValue(cbPJ.RFNOMEFANTASIA);
                sheet.GetRow(5).CreateCell(1).SetCellValue(cbPJ.RFCODIGOAE);
                sheet.GetRow(6).CreateCell(1).SetCellValue(cbPJ.RFDESCRICAOAE);
                sheet.GetRow(7).CreateCell(1).SetCellValue(cbPJ.RFCODIGONJ);
                sheet.GetRow(8).CreateCell(1).SetCellValue(cbPJ.RFDESCRICAONJ);
                sheet.GetRow(9).CreateCell(1).SetCellValue(cbPJ.RFLOGRADOURO);
                sheet.GetRow(10).CreateCell(1).SetCellValue(cbPJ.RFNUMERO);
                sheet.GetRow(11).CreateCell(1).SetCellValue(cbPJ.RFCOMPLEMENTO);
                sheet.GetRow(12).CreateCell(1).SetCellValue(cbPJ.RFCEP);
                sheet.GetRow(13).CreateCell(1).SetCellValue(cbPJ.RFBAIRRO);
                sheet.GetRow(14).CreateCell(1).SetCellValue(cbPJ.RFCIDADE);
                sheet.GetRow(15).CreateCell(1).SetCellValue(cbPJ.RFESTADO);
                sheet.GetRow(16).CreateCell(1).SetCellValue(cbPJ.RFSITUACAO);
                sheet.GetRow(17).CreateCell(1).SetCellValue(cbPJ.RFDATASITUACAO);
                sheet.GetRow(18).CreateCell(1).SetCellValue(cbPJ.RFSITUACAOESPECIAL);
                sheet.GetRow(19).CreateCell(1).SetCellValue(cbPJ.RFDATASITUACAOESPECIAL);
                sheet.GetRow(20).CreateCell(1).SetCellValue(cbPJ.RFDATACONSULTA);
                sheet.GetRow(21).CreateCell(1).SetCellValue(cbPJ.RFHORACONSULTA);
                sheet.GetRow(22).CreateCell(1).SetCellValue(cbPJ.RFTIPOEMPRESA);
                sheet.GetRow(23).CreateCell(1).SetCellValue(cbPJ.RFMOTIVOSITUACAO);

                //SINTEGRA
                sheet = templateWorkbook.GetSheet("SINTEGRA");

                sheet.GetRow(1).CreateCell(1).SetCellValue(cbPJ.SICNPJ);
                sheet.GetRow(2).CreateCell(1).SetCellValue(cbPJ.SIIE);
                sheet.GetRow(3).CreateCell(1).SetCellValue(cbPJ.SIRAZAOSOCIAL);
                sheet.GetRow(4).CreateCell(1).SetCellValue(cbPJ.SINOMEFANTASIA);
                sheet.GetRow(5).CreateCell(1).SetCellValue(cbPJ.SILOGRADOURO);
                sheet.GetRow(6).CreateCell(1).SetCellValue(cbPJ.SINUMERO);
                sheet.GetRow(7).CreateCell(1).SetCellValue(cbPJ.SICOMPLEMENTO);
                sheet.GetRow(8).CreateCell(1).SetCellValue(cbPJ.SIBAIRRO);
                sheet.GetRow(9).CreateCell(1).SetCellValue(cbPJ.SICIDADE);
                sheet.GetRow(10).CreateCell(1).SetCellValue(cbPJ.SIESTADO);
                sheet.GetRow(11).CreateCell(1).SetCellValue(cbPJ.SICEP);
                sheet.GetRow(12).CreateCell(1).SetCellValue(cbPJ.SIEMAIL);
                sheet.GetRow(13).CreateCell(1).SetCellValue(cbPJ.SISITUACAO);
                sheet.GetRow(14).CreateCell(1).SetCellValue(cbPJ.SIDATAINCLUSAO);
                sheet.GetRow(15).CreateCell(1).SetCellValue(cbPJ.SIDATABAIXA);
                sheet.GetRow(16).CreateCell(1).SetCellValue(cbPJ.SIDATACONSULTA);
                sheet.GetRow(17).CreateCell(1).SetCellValue(cbPJ.SINUMEROCONSULTA);
                sheet.GetRow(18).CreateCell(1).SetCellValue(cbPJ.SIREGIMEAPURACAO);
                sheet.GetRow(19).CreateCell(1).SetCellValue(cbPJ.SIMOTIVOBAIXA);
                sheet.GetRow(20).CreateCell(1).SetCellValue(cbPJ.SIREGULARIDADEFISCAL);

                //SIMPLESNACIONAL
                sheet = templateWorkbook.GetSheet("SIMPLESNACIONAL");

                sheet.GetRow(1).CreateCell(1).SetCellValue(cbPJ.SNCNPJ);
                sheet.GetRow(2).CreateCell(1).SetCellValue(cbPJ.SNRAZAOSOCIAL);
                sheet.GetRow(3).CreateCell(1).SetCellValue(cbPJ.SNSITUACAO);
                sheet.GetRow(4).CreateCell(1).SetCellValue(cbPJ.SNDATASITUACAO);
                sheet.GetRow(5).CreateCell(1).SetCellValue(cbPJ.SNSITUACAOSIMEI);
                sheet.GetRow(6).CreateCell(1).SetCellValue(cbPJ.SNSITUACAOANTERIOR);


                //CORREIOS
                sheet = templateWorkbook.GetSheet("CORREIOS");
                sheet.GetRow(1).CreateCell(1).SetCellValue(cbPJ.CORUA);
                sheet.GetRow(2).CreateCell(1).SetCellValue(cbPJ.COCOMPLEMENTO);
                sheet.GetRow(3).CreateCell(1).SetCellValue(cbPJ.COBAIRRO);
                sheet.GetRow(4).CreateCell(1).SetCellValue(cbPJ.COCIDADE);
                sheet.GetRow(5).CreateCell(1).SetCellValue(cbPJ.COESTADO);
                sheet.GetRow(6).CreateCell(1).SetCellValue(cbPJ.COCEPNUM);
                sheet.GetRow(7).CreateCell(1).SetCellValue(cbPJ.COERRO);
                sheet.GetRow(8).CreateCell(1).SetCellValue(cbPJ.COCODLOGRADOURO);
                sheet.GetRow(9).CreateCell(1).SetCellValue(cbPJ.COCODBAIRRO);
                sheet.GetRow(10).CreateCell(1).SetCellValue(cbPJ.COCODLOCALIDADE);
                sheet.GetRow(11).CreateCell(1).SetCellValue(cbPJ.CODIBGE);


                templateWorkbook.Write(msResult);
                msResult.Close();

                templateWorkbook.Dispose();
                templateWorkbook = null;
                msResult.Dispose();
                msResult = null;


                cbPJ.CAMINHOEXCEL = xlsfile;

                registraConsultaBill();
            }
            catch (Exception ex)
            {
                log.erro("CUNI.gerarXLS", idemp.ToString(), ex.Message);
                throw (ex);
            }
        }

        private void registraConsultaBill()
        {
            try
            {
                tb_cuni tbc = new tb_cuni();
                tbc.DataCuni = DateTime.Now;
                tbc.FK_IDemp = idemp;
                db.tb_cunis.InsertOnSubmit(tbc);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                log.erro("cuni.registraconsultaBill", idemp.ToString(), ex.Message);
                throw (ex);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="_tp">Tipo consulta</param>
        /// <param name="_p1">Parametro 1</param>
        /// <param name="_p2">Parametro 2</param>
        /// <param name="_p3">Parametro 3</param>
        /// <param name="_p4">Parametro 4</param>
        /// <param name="_p5">Parametro 5</param>
        /// <returns></returns>
        public string consultar(int _tp, string _p1, string _p2, string _p3, string _p4, string _p5)
        {
            cbPJ = new consultaBasicaPJ();
            cbPJ.OR_CNPJ = _p1;
            cbPJ.OR_IE = _p2;
            cbPJ.OR_UF = _p3;
            bool continuar = true;
            string ret = "";
            long PKcnpj = 0;
            dbiparser.dbiparser dbi = new dbiparser.dbiparser(ConfigurationManager.AppSettings["wsurl"].ToString(), ConfigurationManager.AppSettings["wslogin"].ToString(), ConfigurationManager.AppSettings["wssenha"].ToString());

            switch (_tp)//tipo consulta
            {


                #region consultas 1 a 22
                
                case 1:
                case 2:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        
                            dbiparser.dbiparser.SINTEGRA sin = (dbiparser.dbiparser.SINTEGRA)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                            if (dbi.ERROCONSULTA == null)
                            {
                                gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "SI", sin.PAGINA, _p1);
                                
                                ret = "SI_"+_p1+".html";
                            }
                            else
                            {
                                
                                dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                ret = "ERRO";
                            }

                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 1 e 2", _p1, ex.Message);
                    }

                    break;

                case 3:
               
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                            dbiparser.dbiparser.RFCNPJ cnpj = (dbiparser.dbiparser.RFCNPJ)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                            if (dbi.ERROCONSULTA == null)
                            {
                                gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "RF", cnpj.PAGINA, _p1);
                                ret = "RF_" + _p1 + ".html";
                               
                            }
                            else
                            {
                                dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                
                                ret = "ERRO";
                            }

                        
                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 3", _p1, ex.Message);
                    }


                    break;
                case 4:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                            dbiparser.dbiparser.RFCPF cpf = (dbiparser.dbiparser.RFCPF)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                            if (dbi.ERROCONSULTA == null)
                            {
                                gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "RF", cpf.PAGINA, _p1);
                                ret = "RF_" + _p1 + ".html";
                               
                            }
                            else
                            {
                                dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                
                                ret = "ERRO";
                            }

                        
                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 4", _p1, ex.Message);
                    }
                    break;
                case 5:
                case 6:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.SECFAZENDA sfaz = (dbiparser.dbiparser.SECFAZENDA)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "SF", sfaz.PAGINA, _p1);
                            ret = "SF_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 5", _p1, ex.Message);
                    }
                    break;
                case 7:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.SUFRAMA suf = (dbiparser.dbiparser.SUFRAMA)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "SU", suf.PAGINA, _p1);
                            ret = "SU_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 7", _p1, ex.Message);
                    }
                    break;
                case 9:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.SIMPLES simp = (dbiparser.dbiparser.SIMPLES)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "SN", simp.PAGINA, _p1);
                            ret = "SN_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 9", _p1, ex.Message);
                    }
                    break;
                case 10:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.PREFSAOPAULO psp = (dbiparser.dbiparser.PREFSAOPAULO)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "PS", psp.PAGINA, _p1);
                            ret = "PS_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 10", _p1, ex.Message);
                    }
                    break;
                case 11:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {

                        dbiparser.dbiparser.ANVISA anv = (dbiparser.dbiparser.ANVISA)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        
                        
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "AV", anv.PAGINA, _p1);
                            ret = "AV_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 11", _p1, ex.Message);
                    }
                    break;
                case 12:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.ANP anp = (dbiparser.dbiparser.ANP)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "AP", anp.PAGINA, _p1);
                            ret = "AP_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 12", _p1, ex.Message);
                    }
                    break;
                case 14:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.DATAPREVPIS pis = (dbiparser.dbiparser.DATAPREVPIS)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "PI", pis.PAGINA, _p1);
                            ret = "PI_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 14", _p1, ex.Message);
                    }
                    break;
                case 15:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.CRM crm = (dbiparser.dbiparser.CRM)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "CM", crm.PAGINA, _p1);
                            ret = "CM_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 15", _p1, ex.Message);
                    }
                    break;
                case 18:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.ANTT antt = (dbiparser.dbiparser.ANTT)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "AT", antt.PAGINA, _p1);
                            ret = "AT_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 18", _p1, ex.Message);
                    }
                    break;
                case 19:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "");
                    try
                    {
                        dbiparser.dbiparser.ONFE onfe = (dbiparser.dbiparser.ONFE)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                        if (dbi.ERROCONSULTA == null)
                        {
                            gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "ON", onfe.PAGINA, _p1);
                            ret = "AN_" + _p1 + ".html";

                        }
                        else
                        {
                            dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;

                            ret = "ERRO";
                        }


                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("cuni.consultar, Consulta tipo 19", _p1, ex.Message);
                    }
                    break;
                case 20:
                case 21:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "").Replace(" ", "");
                    try
                    {
                            dbiparser.dbiparser.NFE nf = (dbiparser.dbiparser.NFE)dbi.Consulta(_tp, _p1, _p2, _p3, _p4, _p5);
                            if (dbi.ERROCONSULTA == null)
                            {

                                gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "DA", nf.PAGINA, _p1);

                                ret = "DA_" + _p1 + ".html";
                            }
                            else
                            {
                                dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                ret = "ERRO";
                            }
                    }
                    catch (Exception ex)
                    {
                        ret = "ERRO";
                        log.erro("consDB.consultar, Consulta tipo 20 e 21", _p1, ex.Message);
                    }

                    break;

                #endregion

                case 201:
                    _p1 = _p1.Replace(".", "").Replace("-", "").Replace("/", "").Replace(" ", "");
                    try
                    {
                            dbiparser.dbiparser.RFCNPJ rfPJ = (dbiparser.dbiparser.RFCNPJ)dbi.Consulta(3, _p1, "", "", "", "");
                            if (dbi.ERROCONSULTA == null)
                            {
 
                                cbPJ.RFCNPJ                                    = rfPJ.CNPJ;
                                cbPJ.RFDATAABERTURA                            = rfPJ.DATAABERTURA != null ? rfPJ.DATAABERTURA.ToString("dd/MM/yyyy"): "";
                                cbPJ.RFRAZAOSOCIAL                             = rfPJ.RAZAOSOCIAL;
                                cbPJ.RFNOMEFANTASIA                            = rfPJ.NOMEFANTASIA;
                                cbPJ.RFCODIGOAE                                = rfPJ.CODIGOAE;
                                cbPJ.RFDESCRICAOAE                             = rfPJ.DESCRICAOAE;
                                cbPJ.RFCODIGONJ                                = rfPJ.CODIGONJ;
                                cbPJ.RFDESCRICAONJ                             = rfPJ.DESCRICAONJ;
                                cbPJ.RFLOGRADOURO                              = rfPJ.LOGRADOURO;
                                cbPJ.RFNUMERO                                  = rfPJ.NUMERO;
                                cbPJ.RFCOMPLEMENTO                             = rfPJ.COMPLEMENTO;
                                cbPJ.RFCEP                                     = rfPJ.CEP;
                                cbPJ.RFBAIRRO                                  = rfPJ.BAIRRO;
                                cbPJ.RFCIDADE                                  = rfPJ.CIDADE;
                                cbPJ.RFESTADO                                  = rfPJ.ESTADO;
                                cbPJ.RFSITUACAO                                = rfPJ.SITUACAO;
                                cbPJ.RFDATASITUACAO                            = rfPJ.DATASITUACAO != null ? rfPJ.DATASITUACAO.ToString("dd/MM/yyyy") : "";
                                cbPJ.RFSITUACAOESPECIAL                        = rfPJ.SITUACAOESPECIAL;
                                cbPJ.RFDATASITUACAOESPECIAL                    = rfPJ.DATASITUACAOESPECIAL!= null ? rfPJ.DATASITUACAOESPECIAL.ToString("dd/MM/yyyy") : "";
                                cbPJ.RFDATACONSULTA                            = rfPJ.DATACONSULTA != null ? rfPJ.DATACONSULTA.ToString("dd/MM/yyyy"): "";
                                cbPJ.RFHORACONSULTA                            = rfPJ.HORACONSULTA;
                                cbPJ.RFTIPOEMPRESA                             = rfPJ.TIPOEMPRESA;
                                cbPJ.RFMOTIVOSITUACAO = rfPJ.MOTIVOSITUACAO;
                                
                                //gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "DA", nf.PAGINA, _p1);

                                //ret = "DA_" + _p1 + ".html";
                                cbPJ.ERRCOD = "";

                                // IBGE
                                if (cbPJ.RFCIDADE.Trim() != "")
                                {
                                    dbiparser.dbiparser.MUNICIPIOIBGE muniPJ = (dbiparser.dbiparser.MUNICIPIOIBGE)dbi.Consulta(13, cbPJ.RFCIDADE, cbPJ.RFESTADO, "", "", "");
                                    if (dbi.ERROCONSULTA == null)
                                    {
                                        cbPJ.CODIBGE = muniPJ.CODIGOIBGE;
                                    }
                                    else
                                    {
                                        cbPJ.CODIBGE = "Não Encontrado";

                                    }
                                }
                                else
                                {
                                    cbPJ.CODIBGE = "Cidade em BRANCO";
                                }
                                // Correios
                                if (cbPJ.RFCEP.Trim() != "" && cbPJ.RFCEP.Trim() != "********")
                                {
                                    dbiparser.dbiparser.CEP cepPJ = (dbiparser.dbiparser.CEP)dbi.Consulta(16, cbPJ.RFCEP, "", "", "", "");
                                    if (dbi.ERROCONSULTA == null)
                                    {
                                        cbPJ.CORUA = cepPJ.RUA;
                                        cbPJ.COCOMPLEMENTO = cepPJ.COMPLEMENTO;
                                        cbPJ.COBAIRRO = cepPJ.BAIRRO;
                                        cbPJ.COCIDADE = cepPJ.CIDADE;
                                        cbPJ.COESTADO = cepPJ.ESTADO;
                                        cbPJ.COCEPNUM = cepPJ.CEPNUM;
                                        cbPJ.COERRO = cepPJ.ERRO;
                                        cbPJ.COCODLOGRADOURO = cepPJ.CODLOGRADOURO;
                                        cbPJ.COCODBAIRRO = cepPJ.CODBAIRRO;
                                        cbPJ.COCODLOCALIDADE = cepPJ.CODLOCALIDADE;
                                    }
                                    else
                                    {
                                        cbPJ.COERRO = "Não Encontrado";

                                    }
                                }
                                else
                                {
                                    cbPJ.COERRO = "CEP em BRANCO";
                                }
                            }
                            else
                            {
                                continuar = false;
                                cbPJ.ERRCOD = "RF";
                                cbPJ.ERRDESCRI = dbi.ERROCONSULTA.CODERRO + " - " + dbi.ERROCONSULTA.ERRODESCRI;
                                dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                ret = dbi.ERROCONSULTA.CODERRO + " - " + dbi.ERROCONSULTA.ERRODESCRI;
                            }
                    }
                    catch (Exception ex)
                    {
                        cbPJ.ERRCOD = "RF";
                        cbPJ.ERRDESCRI = ex.Message;
                        continuar = false;
                        ret = "ERRO";
                        log.erro("consDB.consultar, Consulta tipo 101 - RF", _p1, ex.Message);
                    }

                    if (continuar)
                    {
                        dbi = new dbiparser.dbiparser(ConfigurationManager.AppSettings["wsurl"].ToString(), ConfigurationManager.AppSettings["wslogin"].ToString(), ConfigurationManager.AppSettings["wssenha"].ToString());

                        try
                        {
                            dbi.USARLISTASINTEGRA = true;
                            dbiparser.dbiparser.ListaSINTEGRA lsiPJ = (dbiparser.dbiparser.ListaSINTEGRA)dbi.Consulta(1, _p1, cbPJ.RFESTADO, "", "", "");
                            if (dbi.ERROCONSULTA == null)
                            {
                                foreach (dbiparser.dbiparser.SINTEGRA si in lsiPJ.LISTASI)
                                {
                                    cbPJ.SICNPJ = cbPJ.SICNPJ + si.CNPJ + ";";
                                    cbPJ.SIIE = cbPJ.SIIE + si.IE + ";";
                                    cbPJ.SIRAZAOSOCIAL = cbPJ.SIRAZAOSOCIAL + si.RAZAOSOCIAL + ";";
                                    cbPJ.SINOMEFANTASIA = cbPJ.SINOMEFANTASIA + si.NOMEFANTASIA + ";";
                                    cbPJ.SILOGRADOURO = cbPJ.SILOGRADOURO + si.LOGRADOURO + ";";
                                    cbPJ.SINUMERO = cbPJ.SINUMERO + si.NUMERO + ";";
                                    cbPJ.SICOMPLEMENTO = cbPJ.SICOMPLEMENTO + si.COMPLEMENTO + ";";
                                    cbPJ.SIBAIRRO = cbPJ.SIBAIRRO + si.BAIRRO + ";";
                                    cbPJ.SICIDADE = cbPJ.SICIDADE + si.CIDADE + ";";
                                    cbPJ.SIESTADO = cbPJ.SIESTADO + si.ESTADO + ";";
                                    cbPJ.SICEP = cbPJ.SICEP + si.CEP + ";";
                                    cbPJ.SIEMAIL = cbPJ.SIEMAIL + si.EMAIL + ";";
                                    cbPJ.SISITUACAO = cbPJ.SISITUACAO + si.SITUACAO + ";";
                                    cbPJ.SIDATAINCLUSAO = cbPJ.SIDATAINCLUSAO + si.DATAINCLUSAO != null ? si.DATAINCLUSAO.ToString("dd/MM/yyyy") : "" + ";";
                                    cbPJ.SIDATABAIXA = cbPJ.SIDATABAIXA + si.DATABAIXA != null ? si.DATABAIXA.ToString("dd/MM/yyyy") : "" + ";";
                                    cbPJ.SIDATACONSULTA = cbPJ.SIDATACONSULTA + si.DATACONSULTA != null ? si.DATACONSULTA.ToString("dd/MM/yyyy") : "" + ";";
                                    cbPJ.SINUMEROCONSULTA = cbPJ.SINUMEROCONSULTA + si.NUMEROCONSULTA + ";";
                                    cbPJ.SIREGIMEAPURACAO = cbPJ.SIREGIMEAPURACAO + si.REGIMEAPURACAO + ";";
                                    cbPJ.SIMOTIVOBAIXA = cbPJ.SIMOTIVOBAIXA + si.MOTIVOBAIXA + ";";
                                    cbPJ.SIREGULARIDADEFISCAL = cbPJ.SIREGULARIDADEFISCAL + si.REGULARIDADEFISCAL + ";";

                                    cbPJ.SIENQUADRAMENTOEMPRESA = cbPJ.SIENQUADRAMENTOEMPRESA + si.ENQUADRAMENTOEMPRESA + ";";
                                    cbPJ.SICODINCRA = cbPJ.SICODINCRA + si.CODINCRA + ";";
                                    cbPJ.SIEMISSAONFE = cbPJ.SIEMISSAONFE + si.EMISSAONFE + ";";


                                }

                                //gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "DA", nf.PAGINA, _p1);

                                //ret = "DA_" + _p1 + ".html";
                            }
                            else
                            {

                                if (dbi.ERROCONSULTA.CODERRO == "19")
                                {
                                    dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                    cbPJ.SICNPJ = "";
                                    cbPJ.SIIE = "Não sujeito ao ICMS";
                                    cbPJ.SIRAZAOSOCIAL = "";
                                    cbPJ.SINOMEFANTASIA = "";
                                    cbPJ.SILOGRADOURO = "";
                                    cbPJ.SINUMERO = "";
                                    cbPJ.SICOMPLEMENTO = "";
                                    cbPJ.SIBAIRRO = "";
                                    cbPJ.SICIDADE = "";
                                    cbPJ.SIESTADO = "";
                                    cbPJ.SICEP = "";
                                    cbPJ.SIEMAIL = "";
                                    cbPJ.SISITUACAO = err.CODERRO + " - " + err.ERRODESCRI;// cbPJ.SISITUACAO + si.SITUACAO + ";";
                                    cbPJ.SIDATAINCLUSAO = "";
                                    cbPJ.SIDATABAIXA = "";
                                    cbPJ.SIDATACONSULTA = "";
                                    cbPJ.SINUMEROCONSULTA = "";
                                    cbPJ.SIREGIMEAPURACAO = "";
                                    cbPJ.SIMOTIVOBAIXA = "";
                                    cbPJ.SIREGULARIDADEFISCAL = "";
                                    //dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                    // ret = "ERRO";
                                }
                                else
                                {

                                    dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                    cbPJ.SICNPJ = "";
                                    cbPJ.SIIE = "Erro na Consulta";
                                    cbPJ.SIRAZAOSOCIAL = "";
                                    cbPJ.SINOMEFANTASIA = "";
                                    cbPJ.SILOGRADOURO = "";
                                    cbPJ.SINUMERO = "";
                                    cbPJ.SICOMPLEMENTO = "";
                                    cbPJ.SIBAIRRO = "";
                                    cbPJ.SICIDADE = "";
                                    cbPJ.SIESTADO = "";
                                    cbPJ.SICEP = "";
                                    cbPJ.SIEMAIL = "";
                                    cbPJ.SISITUACAO = err.CODERRO + " - " + err.ERRODESCRI;// cbPJ.SISITUACAO + si.SITUACAO + ";";
                                    cbPJ.SIDATAINCLUSAO = "";
                                    cbPJ.SIDATABAIXA = "";
                                    cbPJ.SIDATACONSULTA = "";
                                    cbPJ.SINUMEROCONSULTA = "";
                                    cbPJ.SIREGIMEAPURACAO = "";
                                    cbPJ.SIMOTIVOBAIXA = "";
                                    cbPJ.SIREGULARIDADEFISCAL = "";
                                    //dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                    // ret = "ERRO";
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            cbPJ.SIIE = "Erro na Consulta";
                            cbPJ.SISITUACAO = ex.Message;

                            ret = "ERRO";
                            log.erro("consDB.consultar, Consulta tipo 101 - SI", _p1, ex.Message);
                        }

                        dbi = new dbiparser.dbiparser(ConfigurationManager.AppSettings["wsurl"].ToString(), ConfigurationManager.AppSettings["wslogin"].ToString(), ConfigurationManager.AppSettings["wssenha"].ToString());

                        try
                        {
                            dbiparser.dbiparser.SIMPLES smPJ = (dbiparser.dbiparser.SIMPLES)dbi.Consulta(9, _p1, cbPJ.RFESTADO, "", "", "");
                            if (dbi.ERROCONSULTA == null)
                            {

                                cbPJ.SNCNPJ = smPJ.CNPJ;
                                cbPJ.SNRAZAOSOCIAL = smPJ.RAZAOSOCIAL;
                                cbPJ.SNSITUACAO = smPJ.SITUACAO;
                                cbPJ.SNDATASITUACAO = smPJ.DATASITUACAO != null ? smPJ.DATASITUACAO.ToString("dd/MM/yyyy") : "";
                                cbPJ.SNSITUACAOSIMEI = smPJ.SITUACAOSIMEI;
                                cbPJ.SNSITUACAOANTERIOR = smPJ.SITUACAOANTERIOR;

                                //gravarConprovante(HttpContext.Current.Session["IDuser"].ToString(), HttpContext.Current.Session["IDemp"].ToString(), "DA", nf.PAGINA, _p1);

                                //ret = "DA_" + _p1 + ".html";
                            }
                            else
                            {
                                cbPJ.SNCNPJ = "Erro na Consulta";
                                cbPJ.SNSITUACAO =dbi.ERROCONSULTA.CODERRO + " - " + dbi.ERROCONSULTA.ERRODESCRI;
                                dbiparser.dbiparser.ERRO err = dbi.ERROCONSULTA;
                                ret = "ERRO";
                            }
                        }
                        catch (Exception ex)
                        {
                            cbPJ.SNCNPJ = "Erro na Consulta";
                            cbPJ.SNSITUACAO = ex.Message;

                            ret = "ERRO";
                            log.erro("consDB.consultar, Consulta tipo 101 - SIMPLES", _p1, ex.Message);
                        }

                    }
                    cbPJ.Higienizar();
                    gravarRegistro();
                    break;
            }
            
            return ret;
        }

        private void gravarConprovante(string _iduser, string _idemp, string _tipo, string _html, string _cnpj)
        {
            string caminho = ConfigurationManager.AppSettings["arqPath"];
            cIO io = new cIO();
            io.CriaDir(caminho + "/" + _idemp.ToString() + "/consuni");
            //DEV:Comentei o trecho a abaixo
            //string data = FW.utils.GeraTimeStamp();
            io.NomeArquivo = caminho + "/" + _idemp.ToString() + "/consuni/" + _tipo + "_" + _cnpj + ".html";
            io.Conteudo = _html;
            io.GravarArquivo();
            io = null;
        }

        //DEV: Comentei o trecho abaixo
        //public FW.consultaBasicaPJ CONSULTABASICA
        //{
        //    get { return cbPJ; }
        //}
        //private FW.consultaBasicaPJ cbPJ;
    }

    public class consultaDisponivel
    {
        public int ID { get; set; }
        public string NOME { get; set; }
    }


}